package com.jinshan.edu.controller;


import com.jinshan.edu.entity.EduChapter;
import com.jinshan.edu.service.EduChapterService;
import com.jinshan.response.ChapterVO;
import com.jinshan.response.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Jinshan
 * @since 2020-06-27
 */
@RestController
@RequestMapping("/edu/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    public EduChapterService chapterService;

    // 添加章节
    @PostMapping("/saveChapter")
    public RetVal saveChapter(EduChapter chapter) {
        chapterService.saveChapter(chapter);
        return RetVal.success();
    }

    // 修改章节
    @PutMapping("/updateChapter")
    public RetVal updateChapter(EduChapter chapter) {
        chapterService.updateChapter(chapter);
        return RetVal.success();
    }

    // 删除章节
    @DeleteMapping("/{id}")
    public RetVal deleteChapterById(@PathVariable String id) {
        boolean flag = chapterService.deleteChapterById(id);
        if (flag) {
            return RetVal.success().message("删除章节成功");
        } else {
            return RetVal.error().message("删除章节失败");
        }
    }

    @GetMapping("/{id}")
    public RetVal getChapterById(@PathVariable String id) {
        EduChapter chapter = chapterService.getChapterById(id);
        return RetVal.success().data("chapter", chapter);
    }

    @GetMapping("/getAllChapterByCourseId")
    public RetVal getAllChapterByCourseId(@RequestParam String courseId) {
        List<ChapterVO> chapterVOList = chapterService.getAllChapterByCourseId(courseId);
        return RetVal.success().data("chapterVOList", chapterVOList);
    }
}

